package com.ovhoo.android.smsbombing;




import java.util.prefs.Preferences;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author azalsup
 *
 */
public class Smsbombing extends Activity implements Button.OnClickListener{
	
	private Button boutonenvoie;
	private EditText numeroTelephone;
	private EditText editMessage;
	private TextView viewCounter;
	private boolean envoyer = false;
	
	
	private SendingThread sender;
	
	private ShortMessage message;
	private MessagesHistory history;
	
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			updateCounter();
		}
	};
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        this.boutonenvoie = (Button) findViewById(R.id.SEND_BUTTON);
        this.numeroTelephone = (EditText) findViewById(R.id.PHONE_NUMBER_1);
        this.editMessage = (EditText) findViewById(R.id.MESSAGE_1);
        this.viewCounter = (TextView) findViewById(R.id.MSG_COUNTER);
 
        //listeners
        this.boutonenvoie.setOnClickListener(this);
        

        this.sender = new SendingThread(this, this.message, handler);
        
        this.history = new MessagesHistory();
        
    }
    
	@Override
	public void onClick(View v) {
		this.message = new ShortMessage(this.numeroTelephone.getText().toString(), this.editMessage.getText().toString());
	
		if ((! this.envoyer ) && ( this.message.isAcceptable() )){
			this.envoyer = true;
		}
		else{
			if (! this.envoyer){
				Toast.makeText(getBaseContext(), 
		                "Please enter both phone number and message.", 
		                Toast.LENGTH_SHORT).show();
			}
		
			this.envoyer = false;
			
		}
		
		if (this.envoyer){
			this.boutonenvoie.setText("Stop Sending");
			
			this.history.addMessage(this.message);
			this.sender = new SendingThread(this, this.message, handler);
	        this.sender.start();
		}
		else{
			this.boutonenvoie.setText("Start Sending");
			this.sender.pause();
		}
		
	} 
	
	
    public void onPause(){
    	super.onPause();
    	
		this.boutonenvoie.setText("Start Sending");
		this.envoyer = false;
		this.sender.pause();
    }
    
    public void onResume(){
    	super.onResume();
    	
    	if (this.envoyer){
			this.boutonenvoie.setText("Stop Sending");
		}
		else{
			this.boutonenvoie.setText("Start Sending");
		}
    }
    
    public void setCounter(int counter){
    	this.viewCounter.setText("" + counter);
    }
    
    public void updateCounter(){
    	this.viewCounter.setText(String.format("%04d", this.message.getCounter()));
    }
    
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()) {
        case R.id.MENU_ITEM_PREFERENCES : this.showPreferenceActivity();

    	}
    	return true;
    }

	private void showPreferenceActivity() {
		Intent settingsActivity = new Intent(getBaseContext(), BasicPreferenceActivity.class);
		startActivity(settingsActivity);
	}

}